package org.example.demo3;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author Gong.Yang
 * 任务队列满了才会创建更多的线程
 */
@Slf4j
public class Demo {
    public static void main(String[] args) throws IOException {
        Runnable task = ()->{
            log.info("tag");
            while (true){
            }
        };

        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(5);
        ThreadPoolExecutor pool =
                new ThreadPoolExecutor(2, 3, 1, TimeUnit.MINUTES, workQueue);

        //压入两个任务
        pool.execute(task); //[pool-1-thread-1] INFO org.example.demo3.Demo -- tag
        pool.execute(task);//[pool-1-thread-2] INFO org.example.demo3.Demo -- tag
        System.in.read();

        //压入五个任务
        pool.execute(task);
        pool.execute(task);
        pool.execute(task);
        pool.execute(task);
        pool.execute(task);
        System.in.read(); //任务队列满了 无输出，说明没有新的线程创建

        //压入一个任务  任务队列满了才会创建更多的线程
        pool.execute(task);  //INFO org.example.demo3.Demo -- tag
        System.in.read();

    }
}
